﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using SundUnderviser.ModelLayer;

namespace SundUnderviser.DBLayer
{
    public  class DBLektionsUdFoertHandling
    {
        private SqlCommand dbCmd = null;

        public List<UdfoertHandlinger> FindHandlinger(int pnr)
        {
            string sql = @"select patientnr,  dato, nr,vagt, initialer, handling, tidspunkt, beskrivelse from Lektionsudfoerthandling " +
                           " where patientnr  = " + pnr
                           + "order by dato desc";


            Console.WriteLine("sql stmt = " + sql);

            dbCmd = DBConnection.GetDbCommand(sql);

            IDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();
            List<UdfoertHandlinger> udFoertList = new List<UdfoertHandlinger>();
            UdfoertHandlinger udFoertObj;

            while (dbReader.Read())
            {

                udFoertObj = new UdfoertHandlinger();
                Console.WriteLine("antal felter " + dbReader.FieldCount);
                Patient patientObj = new Patient();
                patientObj.Nr = Convert.ToInt32(dbReader["patientnr"].ToString());
                udFoertObj.Patient = patientObj;
               
                udFoertObj.Nr = Convert.ToInt32(dbReader["nr"].ToString());
                udFoertObj.Dato = DateTime.Parse(dbReader["dato"].ToString());
                udFoertObj.Vagt = dbReader["vagt"].ToString();
                udFoertObj.Init = dbReader["initialer"].ToString();
                udFoertObj.Tidspunkt = dbReader["tidspunkt"].ToString();
                udFoertObj.Handling = dbReader["handling"].ToString();
                udFoertObj.Beskrivelse = dbReader["beskrivelse"].ToString();

                udFoertList.Add(udFoertObj);


            }
            dbReader.Close();
            DBConnection.Close();
            return udFoertList;
        }



        public int InsertUdfoertHandling(UdfoertHandlinger udfoertObj)
        {
            int rc = -1;
            SqlCommand dbCmd = null;
            try
            { // tildel naeste nr
                udfoertObj.Nr = 1;
                // 
                string sql = "select * from lektionsudfoerthandling where patientnr = " + udfoertObj.Patient.Nr;
                SqlCommand dbCmd1 = DBConnection.GetDbCommand(sql);
                IDataReader dbReader;
                dbReader = dbCmd1.ExecuteReader();
                if (dbReader.Read())
                {
                    Console.WriteLine("har fundet nogle");
                    String sql2 = "select max(nr) as nextnr from lektionsudfoerthandling where patientnr = " + udfoertObj.Patient.Nr;
                    DBConnection.Close();
                    SqlCommand dbCmd2 = DBConnection.GetDbCommand(sql2);
                    dbReader = dbCmd2.ExecuteReader();
                    Console.WriteLine("næstenr handlingObj 1: " + udfoertObj.Nr);
                    if (dbReader.Read())
                    {
                        Console.WriteLine("tildel ny nr");
                        if (dbReader["nextnr"] != null)
                        {
                            udfoertObj.Nr = Convert.ToInt32(dbReader["nextnr"].ToString()) + 1;
                            Console.WriteLine("næstenr statusObj 2: " + udfoertObj.Nr);
                        }
                    }

                }

                DBConnection.Close();
                string myDato = "" + udfoertObj.Dato.Year;
                if (udfoertObj.Dato.Month >= 10)
                {
                    myDato = myDato + udfoertObj.Dato.Month;
                }
                else
                {
                    myDato = myDato + "0" + udfoertObj.Dato.Month;
                }

                if (udfoertObj.Dato.Day >= 10)
                {
                    myDato = myDato + udfoertObj.Dato.Day;
                }
                else
                {
                    myDato = myDato + "0" + udfoertObj.Dato.Day;
                }

                Console.WriteLine("Insert journal mydato : " + myDato);


                string sqlInsert = "insert into lektionsudfoerthandling ( patientnr,nr, dato,vagt, initialer, handling, tidspunkt, beskrivelse)" +
                       "values (" 
                       + udfoertObj.Patient.Nr + "," +
                        + udfoertObj.Nr + ",'" +
                       myDato + "','" +
                    //   udfoertObj.Dato + "','" +
                       udfoertObj.Vagt + "','" +
                       udfoertObj.Init + "','" +
                       udfoertObj.Handling + "','" +
                       udfoertObj.Tidspunkt + "','" +
                       udfoertObj.Beskrivelse +  "')";
                Console.WriteLine(sqlInsert);
                dbCmd = DBConnection.GetDbCommand(sqlInsert);


                rc = dbCmd.ExecuteNonQuery();


            }
            catch (Exception insertE)
            {
                Console.WriteLine("Fejl i insert lektions udført handling");
                Console.WriteLine(insertE);
            }
            DBConnection.Close();
            return rc;
        }
    }
}
